//
//  SearchBottomView.m
//  StationManager
//
//  Created by 泽华伟业 on 17/3/21.
//  Copyright © 2017年 Helong. All rights reserved.
//

#import "SearchBottomView.h"
#import "BNRoutePlanModel.h"
#import "BNCoreServices.h"
#import "SearchDetailViewController.h"
#import <BaiduMapKit/BaiduMapAPI_Search/BMKPoiSearch.h>
#import <BaiduMapKit/BaiduMapAPI_Search/BMKShareURLSearch.h>

@interface SearchBottomView ()<BNNaviRoutePlanDelegate,BNNaviUIManagerDelegate,BMKShareURLSearchDelegate>

@property (nonatomic,strong) UILabel *nameLabel;

@property (nonatomic,strong) UILabel *addressLabel;
///详情按钮
@property (nonatomic,strong) UIButton *detailButton;
///导航按钮
@property (nonatomic,strong) UIButton *naviButton;

@end
@implementation SearchBottomView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self nameLabel];
        [self addressLabel];
        [self detailButton];
        [self naviButton];
    }
    return self;
}

- (UILabel *)nameLabel{
    if (!_nameLabel ) {
        _nameLabel = [[UILabel alloc]init];
        [self addSubview:_nameLabel];
        _nameLabel.numberOfLines = 0;
        [_nameLabel setTextColor:[UIColor blackColor]];
        [_nameLabel setFont:TEXT_FONT];
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(20*wl);
            make.top.equalTo(self).offset(10*hl);
            make.right.equalTo(self);
            make.height.mas_equalTo(30*hl);
        }];
    }
    return _nameLabel;
}

- (UILabel *)addressLabel{
    if (!_addressLabel) {
        _addressLabel = [[UILabel alloc]init];
        [self addSubview:_addressLabel];
        _addressLabel.numberOfLines = 0;
        [_addressLabel setTextColor:[UIColor lightGrayColor]];
        [_addressLabel setFont:[UIFont systemFontOfSize:12]];
        [_addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_nameLabel.mas_bottom);
            make.left.right.equalTo(_nameLabel);
            make.height.mas_equalTo(40*hl);
        }];
        UIView *lineView = [[UIView alloc]init];
        [self addSubview:lineView];
        lineView.backgroundColor = [UIColor lightGrayColor];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_addressLabel.mas_bottom);
            make.left.right.equalTo(self);
            make.height.mas_equalTo(1);
        }];
    }
    return _addressLabel;
}

- (UIButton *)detailButton{
    if (!_detailButton) {
        _detailButton = [[UIButton alloc]init];
        [self addSubview:_detailButton];
        [_detailButton.titleLabel setFont:TEXT_FONT];
        [_detailButton setTitle:@"详情" forState:UIControlStateNormal];
        [_detailButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [_detailButton addTarget:self action:@selector(detailButtonDidSelected:) forControlEvents:UIControlEventTouchUpInside];
        [_detailButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_addressLabel.mas_bottom).offset(1);
            make.left.equalTo(self);
            make.bottom.equalTo(self);
            make.width.equalTo(self).dividedBy(2);
        }];
    }
    return _detailButton;
}

- (UIButton *)naviButton{
    if (!_naviButton) {
        _naviButton = [[UIButton alloc]init];
        [self addSubview:_naviButton];
        [_naviButton setTitle:@"导航" forState:UIControlStateNormal];
        [_naviButton.titleLabel setFont:TEXT_FONT];
        [_naviButton addTarget:self action:@selector(navButtonDidSelected:) forControlEvents:UIControlEventTouchUpInside];
        [_naviButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [_naviButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self);
            make.top.equalTo(_addressLabel.mas_bottom).offset(1);
            make.size.equalTo(_detailButton);
        }];
    }
    return _naviButton;
}

- (void)setTextForlabelWithName:(NSString *)name Address:(NSString *)address{
    [self.nameLabel setText:name];
    [self.addressLabel setText:address];
}

#pragma mark -- 点击详情
- (void)detailButtonDidSelected:(UIButton *)sender{
    //发起短串搜索获取poi分享url
    BMKPoiDetailShareURLOption *detailShareUrlSearchOption = [[BMKPoiDetailShareURLOption alloc]init];
    detailShareUrlSearchOption.uid = self.poiInfo.uid;
    BMKShareURLSearch*shareSearch =  [[BMKShareURLSearch alloc]init];
    shareSearch.delegate = self;
    BOOL flag = [shareSearch requestPoiDetailShareURL:detailShareUrlSearchOption];
    if(flag)
    {
        NSLog(@"详情url检索发送成功");
    }
    else
    {
        NSLog(@"详情url检索发送失败");
    }
}

#pragma  mark -- 获取详情网页网址的方法
- (void)onGetPoiDetailShareURLResult:(BMKShareURLSearch *)searcher result:(BMKShareURLResult *)result errorCode:(BMKSearchErrorCode)error{
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此处理正常结果
        HLog(@"%@",result.url);
        SearchDetailViewController *detailVC = [[SearchDetailViewController alloc]init];
        detailVC.url = result.url;
        [[self getController].navigationController pushViewController:detailVC animated:YES];
    }
    else {
        NSLog(@"抱歉，未找到结果");
    }

}

- (UIViewController *)getController{
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            UIViewController *vc =  (UIViewController*)nextResponder;
            return vc;
        }
    }
    return nil;
}
#pragma mark -- 点击导航
- (void)navButtonDidSelected:(UIButton *)snder{
    ///点击导航开始导航
    NSMutableArray *nodesArray = [[NSMutableArray alloc]initWithCapacity:2];
    //起点
    BNRoutePlanNode *startNode = [[BNRoutePlanNode alloc]init];
    startNode.pos = [[BNPosition alloc]init];
    startNode.pos.x = self.location.longitude;
    startNode.pos.y = self.location.latitude;
    startNode.pos.eType = BNCoordinate_BaiduMapSDK;
    [nodesArray addObject:startNode];
    //终点
    BNRoutePlanNode *endNode = [[BNRoutePlanNode alloc]init];
    endNode.pos = [[BNPosition alloc]init];
    endNode.pos.x = self.poiInfo.pt.longitude;
    endNode.pos.y = self.poiInfo.pt.latitude;
    endNode.pos.eType =  BNCoordinate_BaiduMapSDK;
    [nodesArray addObject:endNode];
    //发起路径规划
    [BNCoreServices_RoutePlan startNaviRoutePlan:BNRoutePlanMode_Recommend naviNodes:nodesArray time:nil delegete:self userInfo:nil];

}
#pragma mark -- 路线规划代理方法
//算路成功回调
- (void)routePlanDidFinished:(NSDictionary *)userInfo{
    //路径规划成功，开始导航
    [BNCoreServices_UI showPage:BNaviUI_NormalNavi delegate:self extParams:nil];
}

//算路失败回调
- (void)routePlanDidFailedWithError:(NSError *)error andUserInfo:(NSDictionary *)userInfo
{
    NSLog(@"算路失败");
    switch ([error code]%10000)
    {
        case BNAVI_ROUTEPLAN_ERROR_LOCATIONFAILED:
            NSLog(@"暂时无法获取您的位置,请稍后重试");
            break;
        case BNAVI_ROUTEPLAN_ERROR_ROUTEPLANFAILED:
            NSLog(@"无法发起导航");
            break;
        case BNAVI_ROUTEPLAN_ERROR_LOCATIONSERVICECLOSED:
            NSLog(@"定位服务未开启,请到系统设置中打开定位服务。");
            break;
        case BNAVI_ROUTEPLAN_ERROR_NODESTOONEAR:
            NSLog(@"起终点距离起终点太近");
            break;
        default:
            NSLog(@"算路失败");
            break;
    } }

//算路取消
-(void)routePlanDidUserCanceled:(NSDictionary*)userInfo {
    NSLog(@"算路取消");
}

#pragma mark -- 导航界面代理方法
//第一次进入导航页面会显示导航声明页，导航回调包括退出导航回调和退出导航声明页回调。
//退出导航页面回调
- (void)onExitPage:(BNaviUIType)pageType  extraInfo:(NSDictionary*)extraInfo
{
    if (pageType == BNaviUI_NormalNavi)
    {
        NSLog(@"退出导航");
    }
    else if (pageType == BNaviUI_Declaration)
    {
        NSLog(@"退出导航声明页面");
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
